//  ========== 
//  = 首页  业务JS = 
//  ========== 

var Main = function() {
	mui.init({
		swipeBack: true //启用右滑关闭功能
	});
	this.index = 0; //公告滚动索引
	this.init();
};
Main.prototype = {
	init: function() {
		var _this = this;

		//初始化项目列表
		_this.loadData();
		_this.eventInit();

	},
	eventInit: function() { //绑定事件

		document.getElementById("regularProd").addEventListener('tap', function() { //点点安盈
			openFinanceListTab('financeList.html?index=0', 'financeList', 0);
		});
		document.getElementById("planProd").addEventListener('tap', function() { //点点专享
			openFinanceListTab('financeList.html?index=1', 'financeList', 1);
		});
		document.getElementById("noviceProd").addEventListener('tap', function() { //新手体验
			openTab('financeList.html', 'financeList');
		});
	},
	//公告
	notice: function() {
		var _this = this;
		var el = $('.u-bu-list'),
			el_chd = $('li', el),
			li_len = el_chd.length - 1 //公告条数减去1
			,
			li_ht = el_chd.height(); //滚动高度
		setInterval($.proxy(function() {
			_this._notice_sld(el, li_len, li_ht)
		}, _this), 3000)
	},
	//公告向上滚动
	_notice_sld: function(el, len, ht) {
		var _this = this;
		if(_this.index > len) {
			_this.index = 0
		}
		el.css({
			"transform": "translate3d(0px, " + _this.index * -ht + "px, 0px)"
		})
		_this.index++;
	},
	//加载数据
	loadData: function() {
		var _this = this;

		//公告列表
		var param = {
			lid: 30,
			isMobile: 1,
			page: 1,
			limit: 5
		};
		var noticeUrl = server_url + '/article/newslistArticle.do';
		$.getJSON(noticeUrl, param, function(retData) {
			if(retData.success) {
				var _html = '';
				for(var i = 0; i < retData.result.length; i++) {
					var item = retData.result[i];
					_html += '\n <li class="mui-ellipsis" data-id="' + item.id + '">' + item.title + '</li>';
				}
				$('.u-bu-list').html(_html);
				$('.u-bu-list li').on("tap", function() {
					mui.openWindow('noticeDetail.html?id=' + this.getAttribute('data-id'));
				});
				//初始化公告
				_this.notice();
			}
		});
		//加载首页轮播图
		var url = server_url + '/getbannerismobilelogin.do';
		$.getJSON(url, {}, function(retData) {

			if(retData.success && retData.result.length > 0) {
				/* 额外增加的一个节点(循环轮播：第一个节点是最后一张轮播)*/
				var allHtml = '';
				var url = retData.result[retData.result.length - 1].mobileLink;

				allHtml += '\n <div class="mui-slider-item mui-slider-item-duplicate" data-name="' + retData.result[retData.result.length - 1].name + '" data-url="' + url + '">';
				allHtml += '\n <img src="' + retData.result[retData.result.length - 1].mobileurl + '">';
				allHtml += '\n </div>';
				for(var i = 0; i < retData.result.length; i++) {
					var item = retData.result[i];
					url = item.mobileLink;

					allHtml += '\n	<div class="mui-slider-item" data-name="' + retData.result[i].name + '" data-url="' + url + '">';
					allHtml += '\n<img src="' + item.mobileurl + '">';
					allHtml += '\n</div>';
				}

				url = retData.result[0].mobileLink;
				/*额外增加的一个节点(循环轮播：最后一个节点是第一张轮播)*/
				allHtml += '\n<div class="mui-slider-item mui-slider-item-duplicate" data-name="' + retData.result[0].name + '" data-url="' + url + '">';
				allHtml += '\n<img src="' + retData.result[0].mobileurl + '">';
				allHtml += '\n</div>';
				$(".mui-slider-loop").html(allHtml);

				mui("#slider").slider({ interval: 1000 });
				//初始化轮播图
				$(".mui-slider-item").on('tap', function() {
					var url = this.getAttribute('data-url');
					var openUrl = 'openUrl.html?url=' + encodeURIComponent(url) + '&title=' + encodeURIComponent(this.getAttribute('data-name'));
					mui.openWindow(openUrl);
				})
			}
		});

		//加载点点安盈数据
		var planUrl = server_url + '/creditFlow/financingAgency/listPlBidPlan.do';
		var param = {
			isMobile: 1,
			page: 1,
			limit: 3
		};
		$.getJSON(planUrl, param, function(retData) {
			if(retData.success) {
				var allHtml = '<li class="mui-table-view-cell">点点安盈</li>';
				for(var i = 0; i < retData.result.length; i++) {
					var item = retData.result[i];
					allHtml += '\n <li class="mui-table-view-cell" data-id=' + item.bidId + '>';
					allHtml += '\n <div class="mui-table">';
					allHtml += '\n <div class="mui-table-cell mui-col-xs-10">';
					allHtml += '\n <h4 class="mui-ellipsis m-proj-tit">' + item.bidProName + '</h4>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="mui-table-cell mui-col-xs-2 mui-text-right">';
					if('1' == item.coupon) {
						allHtml += '\n <span class="mui-h5 m-proj-tag tag-yew">券</span>';
					}
					if('1' == item.novice) {
						allHtml += '\n <span class="mui-h5 m-proj-tag tag-ble">新</span>';
					}
					allHtml += '\n </div>';
					allHtml += '\n  </div>';
					allHtml += '\n <div class="m-proj-info">';
					allHtml += '\n <div class="m-proj-box m-proj-lt">';
					allHtml += '\n <div class="m-info-top">';
					allHtml += '\n <span class="m-big">' + item.interestRate + '</span>';
					if(item.addRate && item.addRate > 0) {
						allHtml += '\n <span class="m-small">+' + item.addRate + '</span>';
					}
					allHtml += '\n <span class="m-unit">%</span>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-info-des">年化利率</div>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-proj-box m-proj-cn">';
					allHtml += '\n <div class="m-info-top">';
					var loanLife = item.loanLife;
					var loanLifeNum = loanLife.replace(/[^0-9]/ig, "");
					allHtml += '\n <span class="m-big">' + loanLifeNum + '</span>';

					allHtml += '\n <span class="m-unit">' + loanLife.replace(loanLifeNum, '') + '</span>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-info-des">项目期限</div>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-proj-box m-proj-rt">';
					allHtml += '\n <div class="m-info-top">';
					allHtml += '\n <span class="m-big">' + item.afterMoney + '</span>';
					//allHtml +='\n <span class="m-big">50</span>';
					//allHtml +='\n <span class="m-unit">万</span>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-info-des">可投金额</div>';
					allHtml += '\n </div>';
					allHtml += '\n </div>';
					allHtml += '\n   <div class="m-range-bar z-act">';
					allHtml += '\n <i style="width: ' + item.progress + '%;"></i>';
					allHtml += '\n <div class="m-range-round" style="left: ' + item.progress + '%;">';
					allHtml += '\n 	<div class="round"></div>';
					allHtml += '\n 	</div>';
					allHtml += '\n   </div>';
					allHtml += '\n   <div class="mui-table m-proj-nottom">';
					allHtml += '\n     <div class="mui-table-cell mui-col-xs-10">';
					allHtml += '\n     <h4 class="mui-ellipsis m-proj-tit">' + item.theWayBack + '</h4>';
					allHtml += '\n   </div>';
					allHtml += '\n <div class="mui-table-cell mui-col-xs-2 mui-text-right">';
					allHtml += '\n <span class="mui-h5 m-proj-tag">' + item.progress + '%</span>';
					allHtml += '\n </div>';
					allHtml += '\n   </div>';
					allHtml += '\n </li>';
				}
				$('#G-proj').html(allHtml);
				mui("#G-proj").on('tap', 'li', function() {
					if(this.getAttribute('data-id')) {
						mui.openWindow({
							url: 'bidPlanDetail.html?bidId=' + this.getAttribute('data-id'),
							id: 'bidPlanDetail',
							extras: param
						});
					};
				});
			}
		});
		
		  //加载点点专享数据
		var planSaleUrl = server_url + '/creditFlow/financingAgency/allPlmanagemoneyPlBidSale.do';
		var planSaleParam = {
			isMobile: 1,
			page: 1,
			limit: 3
		};
		$.getJSON(planSaleUrl, planSaleParam, function(retData) {
			if(retData.success) {
				var allHtml = '<li class="mui-table-view-cell">点点专享</li>';
				for(var i = 0; i < retData.result.length; i++) {
					var item = retData.result[i];
					allHtml += '\n <li class="mui-table-view-cell" data-id=' + item.mmplanId + '>';
					allHtml += '\n <div class="mui-table">';
					allHtml += '\n <div class="mui-table-cell mui-col-xs-10">';
					allHtml += '\n <h4 class="mui-ellipsis m-proj-tit">' + item.mmName + '</h4>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="mui-table-cell mui-col-xs-2 mui-text-right">';
					if('1' == item.coupon) {
						allHtml += '\n <span class="mui-h5 m-proj-tag tag-yew">券</span>';
					}
					if('1' == item.novice) {
						allHtml += '\n <span class="mui-h5 m-proj-tag tag-ble">新</span>';
					}
					allHtml += '\n </div>';
					allHtml += '\n  </div>';
					allHtml += '\n <div class="m-proj-info">';
					allHtml += '\n <div class="m-proj-box m-proj-lt">';
					allHtml += '\n <div class="m-info-top">';
					allHtml += '\n <span class="m-big">' + item.yeaRate + '</span>';
					if(item.addRate && item.addRate > 0) {
						allHtml += '\n <span class="m-small">+' + item.addRate + '</span>';
					}
					allHtml += '\n <span class="m-unit">%</span>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-info-des">年化利率</div>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-proj-box m-proj-cn">';
					allHtml += '\n <div class="m-info-top">';
					//var loanLife = item.loanLife;
					//var loanLifeNum = loanLife.replace(/[^0-9]/ig, "");
					allHtml += '\n <span class="m-big">' + item.investlimit + '</span>';

					allHtml += '\n <span class="m-unit">' + '天'+ '</span>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-info-des">项目期限</div>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-proj-box m-proj-rt">';
					allHtml += '\n <div class="m-info-top">';
					allHtml += '\n <span class="m-big">' + item.afterMoney + '</span>';
					//allHtml +='\n <span class="m-big">50</span>';
					//allHtml +='\n <span class="m-unit">万</span>';
					allHtml += '\n </div>';
					allHtml += '\n <div class="m-info-des">可投金额</div>';
					allHtml += '\n </div>';
					allHtml += '\n </div>';
					allHtml += '\n   <div class="m-range-bar z-act">';
					allHtml += '\n <i style="width: ' + item.progress + '%;"></i>';
					allHtml += '\n <div class="m-range-round" style="left: ' + item.progress + '%;">';
					allHtml += '\n 	<div class="round"></div>';
					allHtml += '\n 	</div>';
					allHtml += '\n   </div>';
					allHtml += '\n   <div class="mui-table m-proj-nottom">';
					allHtml += '\n     <div class="mui-table-cell mui-col-xs-10">';
					if('0' == item.isOne) {
						allHtml += '\n <h4 class="mui-ellipsis m-proj-tit">按期收息,到期还本</h4>';
					} else {
						allHtml += '\n <h4 class="mui-ellipsis m-proj-tit">一次性支付利息</h4>';
					}
					allHtml += '\n   </div>';
					allHtml += '\n <div class="mui-table-cell mui-col-xs-2 mui-text-right">';
					allHtml += '\n <span class="mui-h5 m-proj-tag">' + item.progress + '%</span>';
					allHtml += '\n </div>';
					allHtml += '\n   </div>';
					allHtml += '\n </li>';
				}
				$('#G-proj-bid-sale').html(allHtml);
				mui("#G-proj-bid-sale").on('tap', 'li', function() {
					if(this.getAttribute('data-id')) {
						mui.openWindow({
							url: 'bidSaleDetail.html?mmplanId=' + this.getAttribute('data-id'),
							id: 'bidSaleDetail',
							extras: param
						});
					};
				});
			}
		})
	}

}